package ig.util.ejb;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Define caracteristicas y metodos utiles que pueden llegar a ser utilizados en los
 * beans de entidad y session que conformen la aplicacion.
 * @author lfgarcia
 */

public class IGBean {

	public static final String INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
	protected Connection conn = null;
        protected Connection conn1 = null;
	protected PreparedStatement pstmt1 = null;
	protected PreparedStatement pstmt2 = null;
	protected PreparedStatement pstmt3 = null;
	protected PreparedStatement pstmt4 = null;
	protected PreparedStatement pstmt5 = null;

        
	public Connection getConexion(String datasource) throws NamingException, SQLException{
		/*Context ctx = new InitialContext();
		DataSource ds = (DataSource)ctx.lookup("java:/"+datasource);
                Connection c=  ds.getConnection();
                return c;*/
                
                FileInputStream fis = null;
                try {
                    Properties properties = new Properties();
                    properties.load(fis = new FileInputStream(new File("server/"+datasource+"-DS.properties")));
                    Class.forName(properties.getProperty("driver"));
                    return DriverManager.getConnection(properties.getProperty("url"), properties);    
                } catch (SQLException e) {
                    System.out.println(" DataSource no encontrado : METRICADB :"+e.toString());
                    throw e;
                } catch (Exception e) {
                    System.out.println(" DataSource no encontrado : METRICADB :"+e.toString());
                    throw new SQLException(e);
                }finally{
                    try { fis.close(); } catch (Exception e) {}
                }                        
	}
        
}

